package com.fingard.dsp.bank.directbank.rccosd01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Parker on 2019/7/18.
 * @version 1.0
 */
public class RCCOSD01Transaction extends RCCOSD01Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

//            int tmpIntDateFrom = 0;
//            int tmpIntDateTo = 0;//按自然月循环查询
//            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
//                Calendar tmpClndrStepTo = Calendar.getInstance();
//                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
//                //判断是否跨月
//                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
//                    //跨月则设置为月末
//                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
//                } else {
//                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
//                }
                //赋值通用参数
                RCCOSD01Header bankHeader = new RCCOSD01Header();
                bankHeader.serialNo = getQryTransID();
                bankHeader.bsnCode = "QRCB002";

                String startKey = "";
                String endKey = "";
                String hasNexPage = "Y";
                int index = 1;
                while (StringHelper.hasAnyChar(hasNexPage) && hasNexPage.toUpperCase().equals("Y")) {
                    int startDate = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                    int endDate = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
                    WriteBankLogLn("开始查询" + startDate + "-" + endDate + "第" + index++ + "页.");
                    XmlTextWriter xmlWriter = new XmlTextWriter();
                    writeCommonHeader(xmlWriter, bankHeader);
                    XElement tmpXElement = new XElement("Body", "");
                    tmpXElement.addChild(new XElement("PayerAcNo", p_req7516.actNum));
                    tmpXElement.addChild(new XElement("BeginDate", Format.Date8Format.format(tmpCalendarFrom.getTime())));
                    tmpXElement.addChild(new XElement("EndDate", Format.Date8Format.format(tmpCalendarTo.getTime())));
                    tmpXElement.addChild(new XElement("StartKey", startKey));
                    tmpXElement.addChild(new XElement("EndKey", endKey));
                    xmlWriter.writeXElement(tmpXElement);
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndDocument();
                    //发送报文给银行
                    String[] retStrArr = sendToBank(xmlWriter.toXmlString(), bankHeader);
                    /*String[] retStrArr = new String[]{"","<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                            "<Message>\n" +
                            "\t<Head>\n" +
                            "\t\t<IBSReturnCode>000000</IBSReturnCode>\n" +
                            "\t\t<IBSReturnMsg></IBSReturnMsg>\n" +
                            "\t\t<IBSJnlNo>178276909</IBSJnlNo>\n" +
                            "\t\t<serialNo>1909031438540315</serialNo>\n" +
                            "\t</Head>\n" +
                            "\t<Body>\n" +
                            "\t\t<NextData>Y</NextData>\n" +
                            "\t\t<StartKey>000120190902A</StartKey>\n" +
                            "\t\t<EndKey>001020190902A</EndKey>\n" +
                            "\t\t<List>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t<Map>\n" +
                            "\t\t\t\t<oppositeAccNo>22001460300051521811</oppositeAccNo>\n" +
                            "\t\t\t\t<oppositeName>一汽-大众汽车有限公司</oppositeName>\t\t\t\t\n" +
                            "\t\t\t\t<onloanFlag>D</onloanFlag>\n" +
                            "\t\t\t\t<bsnTellNo>999999999999</bsnTellNo>\n" +
                            "\t\t\t\t<accreditTellNo></accreditTellNo>\n" +
                            "\t\t\t\t<amount>1000000.00</amount>\n" +
                            "\t\t\t\t<transferDate>20190902</transferDate>\n" +
                            "\t\t\t\t<transferTime></transferTime>\n" +
                            "\t\t\t\t<balance>2874236.00</balance>\n" +
                            "\t\t\t\t<hostFlowNo>190902239D00000086418920</hostFlowNo>\n" +
                            "\t\t\t\t<payUse>潍坊农商银行南苑支行</payUse>\n" +
                            "\t\t\t\t<summary>自定义</summary>\n" +
                            "\t\t\t</Map>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t<Map>\n" +
                            "\t\t\t\t<oppositeAccNo>201000164052961</oppositeAccNo>\n" +
                            "\t\t\t\t<oppositeName>义乌轲源电子商务有限公司</oppositeName>\t\t\t\t\n" +
                            "\t\t\t\t<onloanFlag>D</onloanFlag>\n" +
                            "\t\t\t\t<bsnTellNo>999999999999</bsnTellNo>\n" +
                            "\t\t\t\t<accreditTellNo></accreditTellNo>\n" +
                            "\t\t\t\t<amount>10200.00</amount>\n" +
                            "\t\t\t\t<transferDate>20190902</transferDate>\n" +
                            "\t\t\t\t<transferTime></transferTime>\n" +
                            "\t\t\t\t<balance>2864036.00</balance>\n" +
                            "\t\t\t\t<hostFlowNo>190902239D00000086418927</hostFlowNo>\n" +
                            "\t\t\t\t<payUse>潍坊农商银行南苑支行</payUse>\n" +
                            "\t\t\t\t<summary>自定义</summary>\n" +
                            "\t\t\t</Map>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t<Map>\n" +
                            "\t\t\t\t<oppositeAccNo>0000300000000181</oppositeAccNo>\n" +
                            "\t\t\t\t<oppositeName>银联商务股份有限公司客户备付金</oppositeName>\t\t\t\t\n" +
                            "\t\t\t\t<onloanFlag>C</onloanFlag>\n" +
                            "\t\t\t\t<bsnTellNo>999999999999</bsnTellNo>\n" +
                            "\t\t\t\t<accreditTellNo></accreditTellNo>\n" +
                            "\t\t\t\t<amount>1957141.67</amount>\n" +
                            "\t\t\t\t<transferDate>20190902</transferDate>\n" +
                            "\t\t\t\t<transferTime></transferTime>\n" +
                            "\t\t\t\t<balance>4821177.67</balance>\n" +
                            "\t\t\t\t<hostFlowNo>190902101D00001803895125</hostFlowNo>\n" +
                            "\t\t\t\t<payUse>省联社资金中心</payUse>\n" +
                            "\t\t\t\t<summary>转帐存入</summary>\n" +
                            "\t\t\t</Map>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t<Map>\n" +
                            "\t\t\t\t<oppositeAccNo>22001460300054521637</oppositeAccNo>\n" +
                            "\t\t\t\t<oppositeName>长春一汽-大众汽车文化有限公司</oppositeName>\t\t\t\t\n" +
                            "\t\t\t\t<onloanFlag>D</onloanFlag>\n" +
                            "\t\t\t\t<bsnTellNo>999999999999</bsnTellNo>\n" +
                            "\t\t\t\t<accreditTellNo></accreditTellNo>\n" +
                            "\t\t\t\t<amount>16524.00</amount>\n" +
                            "\t\t\t\t<transferDate>20190902</transferDate>\n" +
                            "\t\t\t\t<transferTime></transferTime>\n" +
                            "\t\t\t\t<balance>4804653.67</balance>\n" +
                            "\t\t\t\t<hostFlowNo>190902239D00000086484228</hostFlowNo>\n" +
                            "\t\t\t\t<payUse>潍坊农商银行南苑支行</payUse>\n" +
                            "\t\t\t\t<summary>自定义</summary>\n" +
                            "\t\t\t</Map>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t<Map>\n" +
                            "\t\t\t\t<oppositeAccNo>201000164052961</oppositeAccNo>\n" +
                            "\t\t\t\t<oppositeName>义乌轲源电子商务有限公司</oppositeName>\t\t\t\t\n" +
                            "\t\t\t\t<onloanFlag>D</onloanFlag>\n" +
                            "\t\t\t\t<bsnTellNo>999999999999</bsnTellNo>\n" +
                            "\t\t\t\t<accreditTellNo></accreditTellNo>\n" +
                            "\t\t\t\t<amount>14650.00</amount>\n" +
                            "\t\t\t\t<transferDate>20190902</transferDate>\n" +
                            "\t\t\t\t<transferTime></transferTime>\n" +
                            "\t\t\t\t<balance>4790003.67</balance>\n" +
                            "\t\t\t\t<hostFlowNo>190902239D00000086484235</hostFlowNo>\n" +
                            "\t\t\t\t<payUse>潍坊农商银行南苑支行</payUse>\n" +
                            "\t\t\t\t<summary>自定义</summary>\n" +
                            "\t\t\t</Map>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t<Map>\n" +
                            "\t\t\t\t<oppositeAccNo>4642200001819100024727</oppositeAccNo>\n" +
                            "\t\t\t\t<oppositeName>潍坊弘毅文化传播有限公司</oppositeName>\t\t\t\t\n" +
                            "\t\t\t\t<onloanFlag>D</onloanFlag>\n" +
                            "\t\t\t\t<bsnTellNo>999999999999</bsnTellNo>\n" +
                            "\t\t\t\t<accreditTellNo></accreditTellNo>\n" +
                            "\t\t\t\t<amount>3600.00</amount>\n" +
                            "\t\t\t\t<transferDate>20190902</transferDate>\n" +
                            "\t\t\t\t<transferTime></transferTime>\n" +
                            "\t\t\t\t<balance>4786403.67</balance>\n" +
                            "\t\t\t\t<hostFlowNo>190902239D00000086484240</hostFlowNo>\n" +
                            "\t\t\t\t<payUse>潍坊农商银行南苑支行</payUse>\n" +
                            "\t\t\t\t<summary>自定义</summary>\n" +
                            "\t\t\t</Map>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t<Map>\n" +
                            "\t\t\t\t<oppositeAccNo>536902409510801</oppositeAccNo>\n" +
                            "\t\t\t\t<oppositeName>山东蒙恩文化传媒有限公司</oppositeName>\t\t\t\t\n" +
                            "\t\t\t\t<onloanFlag>D</onloanFlag>\n" +
                            "\t\t\t\t<bsnTellNo>999999999999</bsnTellNo>\n" +
                            "\t\t\t\t<accreditTellNo></accreditTellNo>\n" +
                            "\t\t\t\t<amount>5000.00</amount>\n" +
                            "\t\t\t\t<transferDate>20190902</transferDate>\n" +
                            "\t\t\t\t<transferTime></transferTime>\n" +
                            "\t\t\t\t<balance>4781403.67</balance>\n" +
                            "\t\t\t\t<hostFlowNo>190902239D00000086484245</hostFlowNo>\n" +
                            "\t\t\t\t<payUse>潍坊农商银行南苑支行</payUse>\n" +
                            "\t\t\t\t<summary>自定义</summary>\n" +
                            "\t\t\t</Map>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t<Map>\n" +
                            "\t\t\t\t<oppositeAccNo>37050167510800000270</oppositeAccNo>\n" +
                            "\t\t\t\t<oppositeName>山东汇乐创鑫商贸有限公司</oppositeName>\t\t\t\t\n" +
                            "\t\t\t\t<onloanFlag>D</onloanFlag>\n" +
                            "\t\t\t\t<bsnTellNo>999999999999</bsnTellNo>\n" +
                            "\t\t\t\t<accreditTellNo></accreditTellNo>\n" +
                            "\t\t\t\t<amount>5500.00</amount>\n" +
                            "\t\t\t\t<transferDate>20190902</transferDate>\n" +
                            "\t\t\t\t<transferTime></transferTime>\n" +
                            "\t\t\t\t<balance>4775903.67</balance>\n" +
                            "\t\t\t\t<hostFlowNo>190902239D00000086484252</hostFlowNo>\n" +
                            "\t\t\t\t<payUse>潍坊农商银行南苑支行</payUse>\n" +
                            "\t\t\t\t<summary>自定义</summary>\n" +
                            "\t\t\t</Map>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t<Map>\n" +
                            "\t\t\t\t<oppositeAccNo>201000162807425</oppositeAccNo>\n" +
                            "\t\t\t\t<oppositeName>义乌市轲润电子商务有限公司</oppositeName>\t\t\t\t\n" +
                            "\t\t\t\t<onloanFlag>D</onloanFlag>\n" +
                            "\t\t\t\t<bsnTellNo>999999999999</bsnTellNo>\n" +
                            "\t\t\t\t<accreditTellNo></accreditTellNo>\n" +
                            "\t\t\t\t<amount>10700.00</amount>\n" +
                            "\t\t\t\t<transferDate>20190902</transferDate>\n" +
                            "\t\t\t\t<transferTime></transferTime>\n" +
                            "\t\t\t\t<balance>4765203.67</balance>\n" +
                            "\t\t\t\t<hostFlowNo>190902239D00000086484257</hostFlowNo>\n" +
                            "\t\t\t\t<payUse>潍坊农商银行南苑支行</payUse>\n" +
                            "\t\t\t\t<summary>自定义</summary>\n" +
                            "\t\t\t</Map>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t<Map>\n" +
                            "\t\t\t\t<oppositeAccNo>9070107051842050005074</oppositeAccNo>\n" +
                            "\t\t\t\t<oppositeName>潍坊新富丰广告传媒有限公司</oppositeName>\t\t\t\t\n" +
                            "\t\t\t\t<onloanFlag>D</onloanFlag>\n" +
                            "\t\t\t\t<bsnTellNo>999999999999</bsnTellNo>\n" +
                            "\t\t\t\t<accreditTellNo></accreditTellNo>\n" +
                            "\t\t\t\t<amount>3446.00</amount>\n" +
                            "\t\t\t\t<transferDate>20190902</transferDate>\n" +
                            "\t\t\t\t<transferTime></transferTime>\n" +
                            "\t\t\t\t<balance>4761757.67</balance>\n" +
                            "\t\t\t\t<hostFlowNo>190902239D00000086484263</hostFlowNo>\n" +
                            "\t\t\t\t<payUse>潍坊农商银行南苑支行</payUse>\n" +
                            "\t\t\t\t<summary>行内转帐</summary>\n" +
                            "\t\t\t</Map>\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t\t\n" +
                            "\t\t</List>\n" +
                            "\t\t<RepReserved1></RepReserved1>\n" +
                            "\t\t<RepReserved2></RepReserved2>\n" +
                            "\t</Body>\n" +
                            "</Message>"};*/
                    if (retStrArr[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                            String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");//返回码
                            String tmpRetInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");//返回信息
                            if (tmpRetCode != null && "000000".equals(tmpRetCode)) {
                                hasNexPage = tmpBkReader.getFirstText("/Message/Body/NextData");
                                startKey = tmpBkReader.getFirstText("/Message/Body/StartKey");
                                endKey = tmpBkReader.getFirstText("/Message/Body/EndKey");
                                while (tmpBkReader.read("/Message/Body/List/Map")) {
                                    // 明细内容处理
                                    RetAtsTransDetail detail = new RetAtsTransDetail();
                                    detail.actNum = p_req7516.actNum;
                                    detail.recAccNum = tmpBkReader.readedXElement.getSingleText("oppositeAccNo");
                                    detail.recAccName = tmpBkReader.readedXElement.getSingleText("oppositeName");
                                    //交易日期
                                    String dealDate = tmpBkReader.readedXElement.getSingleText("transferDate");
                                    if (dealDate.length() == 8) {
                                        detail.tranDate = dealDate.substring(0, 4) + "-" + dealDate.substring(4, 6) + "-" + dealDate.substring(6, 8);
                                    }
                                    //交易时间
                                    String strTime = tmpBkReader.readedXElement.getSingleText("transferTime");
                                    if (StringHelper.hasAnyChar(strTime)) {
                                        detail.tranTime = detail.tranDate + " " + strTime.substring(0, 2) + ":" + strTime.substring(2, 4) + ":" + strTime.substring(4, 6);
                                    } else {
                                        detail.tranTime = detail.tranDate + " 00:00:00";
                                    }
                                    //交易日期作为起息日期
                                    detail.valueDate = detail.tranDate;
                                    //交易金额取绝对值
                                    BigDecimal tmpTranAmt = new BigDecimal(tmpBkReader.readedXElement.getSingleText("amount")).abs();
                                    detail.tranAmt = tmpTranAmt.toString();
                                    String tmpCDFlag = tmpBkReader.readedXElement.getSingleText("onloanFlag");
                                    if ("D".equals(tmpCDFlag)) {
                                        if (tmpTranAmt.compareTo(new BigDecimal(0)) == -1) {
                                            detail.cdFlag = "2";
                                        } else {
                                            detail.cdFlag = "1";
                                        }
                                    } else if ("C".equals(tmpCDFlag)) {
                                        if (tmpTranAmt.compareTo(new BigDecimal(0)) == -1) {
                                            detail.cdFlag = "1";
                                        } else {
                                            detail.cdFlag = "2";
                                        }
                                    } else {
                                        tmpRespCode = "-1";
                                        tmpRespInfo = "借贷标志(" + tmpCDFlag + ")无法识别，请与银行核对";
                                        retAts = new RetAtsTrans(p_req7516.reqHeader);
                                        retAts.writeStart();//写返回ATS的统一头部信息
                                        retAts.writeRespCode(tmpRespCode);
                                        retAts.writeRespInfo(tmpRespInfo);
                                        WriteBankLogLn(tmpRespCode + ":" + tmpRespInfo);
                                        retAts.writeEnd();
                                        return retAts;
                                    }
                                    detail.accBal = tmpBkReader.readedXElement.getSingleText("balance");
                                    detail.usage = tmpBkReader.readedXElement.getSingleText("payUse");//用途
                                    detail.payCode = tmpBkReader.readedXElement.getSingleText("summary");//摘要描述
                                    detail.abstr = pickupAbstr(detail.usage);//对账码
                                    detail.cur = p_req7516.actCur;
                                    retAts.writeRetDetail(detail);
                                }
                            } else{
                                tmpRespCode = "-1";
                                tmpRespInfo = tmpRetInfo;
                                retAts = new RetAtsTrans(p_req7516.reqHeader);
                                retAts.writeStart();//写返回ATS的统一头部信息
                                retAts.writeRespCode(tmpRespCode);
                                retAts.writeRespInfo(tmpRespInfo);
                                WriteBankLogLn(tmpRespCode + ":" + tmpRespInfo);
                                retAts.writeEnd();
                                return retAts;
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpSbMsg.toString();
                            retAts = new RetAtsTrans(p_req7516.reqHeader);
                            retAts.writeStart();//写返回ATS的统一头部信息
                            retAts.writeRespCode(tmpRespCode);
                            retAts.writeRespInfo(tmpRespInfo);
                            WriteBankLogLn(tmpRespCode + ":" + tmpRespInfo);
                            retAts.writeEnd();
                            return retAts;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = retStrArr[1];
                        retAts = new RetAtsTrans(p_req7516.reqHeader);
                        retAts.writeStart();//写返回ATS的统一头部信息
                        retAts.writeRespCode(tmpRespCode);
                        retAts.writeRespInfo(tmpRespInfo);
                        WriteBankLogLn(tmpRespCode + ":" + tmpRespInfo);
                        retAts.writeEnd();
                        return retAts;
                    }
                }
//                tmpCalendarFrom = tmpClndrStepTo;
//                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
//                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
//                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
//            }
        } catch (Exception e) {
            e.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = "查询失败：" + e.getMessage();
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
            WriteBankLogLn(tmpRespCode + ":" + tmpRespInfo);
            retAts.writeEnd();
            return retAts;
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;//设置开始时间和结束时间都为当天
        tmpReq7516.dateTo = tmpStrToday;
        return d_7516_HisTrans(tmpReq7516);
    }
}
